﻿<rules>
  <rule id="SvcDoc.Core.1002" category="core" target="servicedoc" specificationsection ="2.2.6.2.7" requirementlevel="must" format="xml" metadata="true">
    <description>A data service MUST represent each EntitySet in its associated Entity Data Model as an &lt;app:collection&gt; element.</description>
    <action processor="xslt+rng">
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
        <xsl:template match="/">
          <grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app">
            <start>
              <element>
                <!-- Ignore the service level element -->
                <anyName />
                <ref name="anyAttributes" />
                <interleave>
                  <element>
                    <!-- Ignore the workspace level element -->
                    <anyName />
                    <ref name="anyAttributes" />
                    <element>
                      <!-- Ignore the title element for default -->
                      <anyName />
                      <ref name="anyAttributes" />
                      <text />
                    </element>
                    <group>
                      <!-- Apply XSLT to get all EntitySet from Metadata and represent them as collections -->
                      <xsl:apply-templates select="*[local-name()='Edmx']/*[local-name()='DataServices']/*[local-name()='Schema']/*[local-name()='EntityContainer']"/>
                    </group>
                  </element>
                  <zeroOrMore>
                    <ref name="anyElement_other_than_app_workspace" />
                  </zeroOrMore>
                </interleave>
              </element>
            </start>
            <define name="anyAttributes">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                </choice>
              </zeroOrMore>
            </define>
            <define name="anyElement">
              <element>
                <anyName />
                <zeroOrMore>
                  <choice>
                    <attribute>
                      <anyName />
                    </attribute>
                    <text />
                    <ref name="anyElement" />
                  </choice>
                </zeroOrMore>
              </element>
            </define>
            <define name="anyContent">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName />
                  </attribute>
                  <text />
                  <ref name="anyElement" />
                </choice>
              </zeroOrMore>
            </define>
            <define name="anyElement_other_than_app_collection">
              <element>
                <choice>
                  <anyName>
                    <except>
                      <nsName ns="http://www.w3.org/2007/app" />
                    </except>
                  </anyName>
                  <nsName ns="http://www.w3.org/2007/app">
                    <except>
                      <name>app:collection</name>
                    </except>
                  </nsName>
                </choice>
                <ref name="anyContent" />
              </element>
            </define>
            <define name="anyElement_other_than_app_workspace">
              <element>
                <choice>
                  <anyName>
                    <except>
                      <nsName ns="http://www.w3.org/2007/app" />
                    </except>
                  </anyName>
                  <nsName ns="http://www.w3.org/2007/app">
                    <except>
                      <name>app:workspace</name>
                    </except>
                  </nsName>
                </choice>
                <ref name="anyAttributes" />
                <zeroOrMore>
                  <ref name="anyElement_other_than_app_collection" />
                </zeroOrMore>
              </element>
            </define>
            <define name="anyElement_other_than_atom_title">
              <element>
                <choice>
                  <anyName>
                    <except>
                      <nsName ns="http://www.w3.org/2005/Atom" />
                    </except>
                  </anyName>
                  <nsName ns="http://www.w3.org/2005/Atom">
                    <except>
                      <name>atom:title</name>
                    </except>
                  </nsName>
                </choice>
                <ref name="anyContent" />
              </element>
            </define>
            <define name="anyAttributes_other_than_atom_href">
              <zeroOrMore>
                <choice>
                  <attribute>
                    <anyName>
                      <except>
                        <name>href</name>
                      </except>
                    </anyName>
                  </attribute>
                </choice>
              </zeroOrMore>
            </define>
          </grammar>
        </xsl:template>
        <xsl:template match="*[local-name()='Edmx']/*[local-name()='DataServices']/*[local-name()='Schema']/*[local-name()='EntityContainer']" xmlns="http://relaxng.org/ns/structure/1.0">
          <xsl:for-each select="*[local-name()='EntitySet']">
            <element name="app:collection">
              <attribute name="href">
                <text />
              </attribute>
              <ref name="anyAttributes_other_than_atom_href" />
              <interleave>
                <element name="atom:title">
                  <ref name="anyAttributes" />
                  <text/>
                </element>
                <zeroOrMore>
                  <ref name="anyElement_other_than_atom_title" />
                </zeroOrMore>
              </interleave>
            </element>
          </xsl:for-each>
        </xsl:template>
      </xsl:stylesheet>
    </action>
    <errormessage>A data service MUST represent each EntitySet in its associated Entity Data Model as an &lt;app:collection&gt; element.</errormessage>
  </rule>
</rules>